﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Common;
using log4net;
using CashFlow.Web.Def;
using CashFlow.Db.DAL;
using CashFlow.Db.Entity;
using CashFlow.Db.Repository;
using CashFlow.Web.Service;

namespace CashFlow.Web.Service.Implement
{
    public class ShopService : MyService<ShopInfoEntity>, IShopService
    {
        public ShopInfoEntity QueryShopInfoByReferenceInvitationCode(string invitationCode)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var entity = db.Queryable<ShopInfoEntity>()
                                .InnerJoin<ShopUserEntity>((si, su) => si.ShopManagerUserId == su.ShopManagerUserId)
                                .InnerJoin<UserInfoEntity>((si, su, u) => su.UserId == u.Id)
                                .Where((si, su, u) => u.InvitationCode == invitationCode)
                                .Select((si, su, u) => si)
                                .First();
                return entity;
            }
        }

        public ShopInfoEntity QueryShopInfoByUserId(int userId)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var entity = db.Queryable<ShopInfoEntity>()
                                .InnerJoin<ShopUserEntity>((si, su) => si.ShopManagerUserId == su.ShopManagerUserId)
                                .Where((si, su) => su.UserId == userId)
                                .Select((si, su) => si)
                                .First();
                return entity;
            }
        }
    }
}
